// #ifdef H5
export class AppUtils {
  constructor() {}
  getPlatform() {
    const u = navigator.userAgent;
    const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) return "android";
    if (isiOS) return "ios";
    return "unknown";
  }
  callFnc(name, data) {
    if (this.getPlatform() === "android") {
      console.log("android====", injectedObject);
      console.log(injectedObject[name](data));
      injectedObject[name](data)
    } else {
      this.setupWebViewJavascriptBridge((bridge) => {
        bridge.callHandler(name, data, (res) => {
          document.getElementById("returnValue").value = res;
        });
      });
    }
    // this.setupWebViewJavascriptBridge((bridge) => {
    //   bridge.callHandler(name, data, (res) => {
    //     console.log("callFnc===", name, data, res);
    //     document.getElementById("returnValue").value = res;
    //   });
    // });
  }
  setupWebViewJavascriptBridge(cb) {
    console.log(
      "setupWebViewJavascriptBridge===",
      cb,
      window?.WebViewJavascriptBridge,
      window?.WVJBCallbacks
    );
    if (window.WebViewJavascriptBridge) {
      return cb(WebViewJavascriptBridge);
    }
    if (window.WVJBCallbacks) {
      return window.WVJBCallbacks.push(cb);
    }
    window.WVJBCallbacks = [cb]; // 创建一个 WVJBCallbacks 全局属性数组，并将 callback 插入到数组中。
    let WVJBIframe = document.createElement("iframe"); // 创建一个 iframe 元素
    WVJBIframe.style.display = "none"; // 不显示
    WVJBIframe.src = "wvjbscheme://__BRIDGE_LOADED__"; // 设置 iframe 的 src 属性
    document.documentElement.appendChild(WVJBIframe); // 把 iframe 添加到当前文导航上。
    setTimeout(function () {
      document.documentElement.removeChild(WVJBIframe);
    }, 0);
  }
}
// #endif
